package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LotBindAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class GrindingStockActivity extends BaseDialogActivity {


    @BindView(R.id.tvLotTitle)
    TextView tvLotTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etWarehouseId)
    FreeUI_ClearEditText etWarehouseId;
    @BindView(R.id.etBingLotID)
    FreeUI_ClearEditText etBingLotID;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.flMain)
    FrameLayout flMain;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private LotBindAdapter mLotBindAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_grinding_stock;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(GrindingStockActivity.this));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(GrindingStockActivity.this, 1));
        mLotBindAdapter = new LotBindAdapter(null);
        mLotBindAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotBindAdapter.isFirstOnly(true);
        rvThingList.setAdapter(mLotBindAdapter);
    }

    @Override
    protected void initData() {

        freeUI_titleView.setRightListener("提交", s ->{
            if (etLotId.getText().toString().equals("")){
                showToast("请扫描批次号",false);
                return;
            }
            if (mLotBindAdapter.getData().size() == 0){
                showToast("请扫描添加载具",false);
                return;
            }
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject1 = new JSONObject();
            try {
                jsonObject1.put("ACTIONTYPE", "");
                jsonObject1.put("WAREHOUSEID", "WAFER_ONLINE_STOCK");
                jsonObject1.put("STORAGEID", etWarehouseId.getText().toString());
                jsonObject1.put("LOTID", etLotId.getText().toString());
                jsonObject1.put("CARRIERID", mLotBindAdapter.getData().toString().replace("[","").replace("]","").replace(" ",""));
                jsonObject.put("Data",jsonObject1);
                jsonObject.put("MessageName","EUIT.LOTMHSHIP");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                    new TypeToken<ResponseBean<Object>>() {
                    }.getType(), getSupportFragmentManager(), this) {
                @Override
                public void onSuccess(Response<ResponseBean<Object>> response) {
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        showToast("入库成功", true);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, 2000);
                    }
                }

                @Override
                public void onError(Response<ResponseBean<Object>> response) {
                    super.onError(response);
                    loadError(response.getException(), "LOTMHSHIP");
                }
            });
        });

        etBingLotID.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (mLotBindAdapter.getData().contains(s.toString())){
                        showToast("当前载具已添加",false);
                    }else {
                        mLotBindAdapter.addData(s.toString());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("划片入库");
        return new FreeUI_AddViewEntity("header", llMain);
    }
}

